package top.wettis.chat.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import cn.hutool.json.JSONObject;
import org.springframework.web.bind.annotation.*;
import top.wettis.chat.pojo.DO.User;
import top.wettis.chat.pojo.Query.*;
import top.wettis.chat.pojo.VO.Result;
import top.wettis.chat.service.impl.UserServiceImpl;

import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author wettis
 * @since 2023-03-25
 */
@RestController
@CrossOrigin
@RequestMapping("/user")
public class UserController {
    private final UserServiceImpl userService;

    public UserController(UserServiceImpl userService) {
        this.userService = userService;
    }

    @PostMapping("/login")
    public SaResult Login(@RequestBody UserAccount userAccount) {
        return userService.doLogin(userAccount);
    }

    @GetMapping("/isLogin")
    public Result<Object> IsLogin(){
        return userService.isLogin();
    }

    @GetMapping("/logout")
    public Result<Object> Logout(){
        return userService.logout();
    }

    @GetMapping("/tokenInfo")
    public Result<Object> TokenInfo(){
        return userService.tokenInfo();
    }

    @GetMapping("/register")
    public SaResult Regster(@RequestParam("name") String name , @RequestParam("password") String password){
        UserAccount userAccount = new UserAccount();
        userAccount.setName(name);
        userAccount.setPassword(password);
        return userService.register(userAccount);
    }

    @PostMapping("/delete")
    public Result<Object> Regster(){
        int user_id = StpUtil.getLoginIdAsInt();
        return userService.delete(user_id);
    }

    @PostMapping("/update")
    public Result<Object> Update(@RequestBody UpdateAccont updateAccont){
        return userService.update(updateAccont);
    }

    @GetMapping("/getUserInfo")
    public Result<Object> Update(){
        try {
            int id = StpUtil.getLoginIdAsInt();
            User user = userService.getUser(id);
            return Result.customInfo(200 , "获取成功" , user);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @GetMapping("/planTime")
    public Result<Object> planTime(){
        try {
            int id = StpUtil.getLoginIdAsInt();
            String time = userService.selectTiime(id);
            return Result.customInfo(200 , "获取成功" , time);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @GetMapping("/pointOrd")
    public List<PointOrdAccont> pointOrd(){
        return userService.pointOrd();
    }
    @GetMapping("/ConOrd")
    public List<ConOrdAccont> ConOrd(){
        return userService.ConOrd();
    }

    @GetMapping("/AskOrd")
    public List<AskOrdAccont> AskOrd(){
        return userService.AskOrd();
    }
    @GetMapping("/PunchTmesOrd")
    public List<PunchOrdAccont> PunchTmesOrd(){
        return userService.PunchTmesOrd();
    }
}
